﻿using EasyHttp.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Text;

namespace EasyPayDemo
{
    public class OrderQuery
    {
        //标记生产还是测试环境
        public static bool isTest = true;

        //接口文档中的方法名
        private const string service = "midend.merchant.checkemail";

        //商户号
        private string merchant_id = EasyPayConfig.TEST_DEFAULT_MERCHANT_ID;

        //接入机构号
        private string partner = EasyPayConfig.TEST_DEFAULT_PARTNER;

        //请求地址
        private string url = EasyPayConfig.TEST_URL;

        //key密钥
        private string key = EasyPayConfig.TEST_MERCHANT_PRIVATE_KEY;

        //加密密钥
        private string DES_ENCODE_KEY = EasyPayConfig.TEST_DES_ENCODE_KEY;

        public void Excute()
        {
            M_Biz_content biz_Content = new M_Biz_content();
            biz_Content.merchant_id = merchant_id;
            biz_Content.out_trade_no = "3888296472";
            dynamic biz = new ExpandoObject();
            M_OrderQuery order = new M_OrderQuery();
            order.partner = partner;
            order.service = service;
            order.sign_type = EasyPayConfig.DEFAULT_ENCODE_TYPE;
            order.charset = EasyPayConfig.DEFAULT_CHARSET;
            biz.merchant_email = "116572814@qq.com";
            order.biz_content = JsonConvert.SerializeObject(biz,Formatting.None);

            
            //var sign = RSAUtil_PKCS.SignByPrivateKey(order.biz_content, key, "UTF-8");
            var sign = EncryptionUtil.RSASignJavaBouncyCastle(order.biz_content, key);


            Console.WriteLine(string.Format("源字符：[{0}]\r\n签名：[{1}]", order.biz_content, sign));
            
            order.sign = sign;


            #region Http请求处理
            HttpClient httpClient = new HttpClient(new EncoderDecoderConfiguration());
            httpClient.Request.AcceptCharSet = "UTF-8";
            var response = httpClient.Post(EasyPayConfig.TEST_URL, order, HttpContentTypes.ApplicationXWwwFormUrlEncoded);
            var result = response.RawText;
            #endregion

            Console.WriteLine(string.Format("返回结果：[{0}]",result));
            var M_result = JsonConvert.DeserializeObject<M_OrderQueryResult>(result);

            var resultData = JsonConvert.SerializeObject(M_result.easypay_merchant_query_response);
            

            
            

            //var resultSignChk = RSAUtil_PKCS.SignCheck(resultData, M_result.sign, EasyPayConfig.TEST_EASYPAY_PUBLIC_KEY);
            var resultSignChk = EncryptionUtil.VerifyJavaBouncyCastle(resultData, EasyPayConfig.TEST_EASYPAY_PUBLIC_KEY, M_result.sign);
            
            if (resultSignChk)
            {
                Console.WriteLine("返回数据正确");
            }
            else
            {
                Console.WriteLine("返回数据被篡改");
            }

            //var resultSign = RSAUtil_PKCS.SignByPublicKey(easy, EasyPayConfig.TEST_EASYPAY_PUBLIC_KEY, "UTF-8");
            //var resultSign = EncryptionUtil.VerifyJavaBouncyCastle(easy, EasyPayConfig.TEST_EASYPAY_PUBLIC_KEY, M_result.sign);
            //var resultSign = EncryptionUtil.ValidationPublicKey(easy, M_result.sign, EasyPayConfig.TEST_EASYPAY_PUBLIC_KEY);


            Console.ReadKey();
            
        }
    }
}
